package com.example.propertymanagement.controller;

import com.example.propertymanagement.dto.PetDTO;
import com.example.propertymanagement.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * PetController：宠物信息的控制层，负责接收前端请求并返回响应
 */
@RestController
@RequestMapping("/api/pets")
public class PetController {

    @Autowired
    private PetService petService;

    /**
     * 获取所有宠物信息
     * @return ResponseEntity<List<PetDTO>> 宠物信息列表
     */
    @GetMapping
    public ResponseEntity<List<PetDTO>> getAllPets() {
        List<PetDTO> pets = petService.getAllPets();
        return ResponseEntity.ok(pets);
    }

    /**
     * 根据业主ID获取宠物信息
     * @param ownerId 业主ID
     * @return ResponseEntity<List<PetDTO>> 宠物信息列表
     */
    @GetMapping("/owner/{ownerId}")
    public ResponseEntity<List<PetDTO>> getPetsByOwnerId(@PathVariable Integer ownerId) {
        List<PetDTO> pets = petService.getPetsByOwnerId(ownerId);
        return ResponseEntity.ok(pets);
    }

    /**
     * 添加新宠物信息
     * @param petDTO 宠物信息
     * @return ResponseEntity<PetDTO> 新添加的宠物信息
     */
    @PostMapping
    public ResponseEntity<PetDTO> addPet(@RequestBody PetDTO petDTO) {
        PetDTO createdPet = petService.addPet(petDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdPet);
    }

    /**
     * 更新宠物信息
     * @param petId 宠物ID
     * @param petDTO 更新后的宠物信息
     * @return ResponseEntity<PetDTO> 更新后的宠物信息
     */
    @PutMapping("/{petId}")
    public ResponseEntity<PetDTO> updatePet(@PathVariable Integer petId, @RequestBody PetDTO petDTO) {
        PetDTO updatedPet = petService.updatePet(petId, petDTO);
        return updatedPet != null ? ResponseEntity.ok(updatedPet) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }

    /**
     * 删除宠物
     * @param petId 宠物ID
     * @return ResponseEntity<Void> 响应结果
     */
    @DeleteMapping("/{petId}")
    public ResponseEntity<Void> deletePet(@PathVariable Integer petId) {
        petService.deletePet(petId);
        return ResponseEntity.noContent().build();
    }
}